<?php
class Comments extends CActiveRecord {
	public $verifyCode;
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('Comments');
	}
	
	public function rules() {
		return array(
			array('postId, commentContent', 'required', 'message' => '评论内容不能为空。'),
			array('verifyCode', 'captcha', 'on' => 'insert', 'message' => '您填写的验证码有误。')
//			array('commentIp', 'length', 'is' => 15)
		);
	}
	
	public function safeAtrributes() {
		
	}
	
	public function relations() {
		return array(
			'post' => array(self::BELONGS_TO, 'Posts', 'postId'),
			'author' => array(self::BELONGS_TO, 'ForumMembers', 'userId',
				'joinType' => 'LEFT JOIN'
			)
		);
	}
	
	public function isByUser() {
		return $this->userId > 0;
	}
	
	public function author() {
		return $this->isByUser() && $this->author !== null ? $this->author->userName() : ($this->commenter != '' ? $this->commenter : '匿名');
	}
}
